package com.lanbu.chat.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lanbu.common.annotation.DataSource;
import com.lanbu.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanbu.common.annotation.Log;
import com.lanbu.common.core.controller.BaseController;
import com.lanbu.common.core.domain.AjaxResult;
import com.lanbu.common.enums.BusinessType;
import com.lanbu.chat.domain.ImGroup;
import com.lanbu.chat.service.IImGroupService;
import com.lanbu.common.utils.poi.ExcelUtil;
import com.lanbu.common.core.page.TableDataInfo;

/**
 * 群Controller
 *
 * @author lanbu
 * @date 2024-09-15
 */
@Api(tags = "群Controller")
@RestController
@RequestMapping("/chat/group")
public class ImGroupController extends BaseController
{
    @Autowired
    private IImGroupService imGroupService;


    @ApiOperation("查询群列表")
    @PreAuthorize("@ss.hasPermi('chat:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImGroup imGroup)
    {
        startPage();
        List<ImGroup> list = imGroupService.selectImGroupList(imGroup);
        return getDataTable(list);
    }


    @ApiOperation("导出群列表")
    @PreAuthorize("@ss.hasPermi('chat:group:export')")
    @Log(title = "群", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImGroup imGroup)
    {
        List<ImGroup> list = imGroupService.selectImGroupList(imGroup);
        ExcelUtil<ImGroup> util = new ExcelUtil<ImGroup>(ImGroup.class);
        util.exportExcel(response, list, "群数据");
    }


    @ApiOperation("获取群详细信息")
    @PreAuthorize("@ss.hasPermi('chat:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        imGroupService.checkGroupDataScope(id);
        return success(imGroupService.selectImGroupById(id));
    }


    @ApiOperation("新增群")
    @PreAuthorize("@ss.hasPermi('chat:group:add')")
    @Log(title = "群", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImGroup imGroup)
    {
        return toAjax(imGroupService.insertImGroup(imGroup));
    }

    @ApiOperation("修改群")
    @PreAuthorize("@ss.hasPermi('chat:group:edit')")
    @Log(title = "群", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImGroup imGroup)
    {
        imGroupService.checkGroupDataScope(imGroup.getId());
        return toAjax(imGroupService.updateImGroup(imGroup));
    }


    @ApiOperation("删除群")
    @PreAuthorize("@ss.hasPermi('chat:group:remove')")
    @Log(title = "群", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imGroupService.deleteImGroupByIds(ids));
    }
}
